<?php
App::uses('AppController', 'Controller');
/**
 * Uploads Controller
 *
 * @property Upload $Upload
 */
class UploadsController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Upload->recursive = 0;
		$this->set('uploads', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Upload->exists($id)) {
			throw new NotFoundException(__('Invalid upload'));
		}
		$options = array('conditions' => array('Upload.' . $this->Upload->primaryKey => $id));
		$this->set('upload', $this->Upload->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Upload->create();
			if ($this->uploadFile() && $this->Upload->save($this->request->data)) {
				$this->Session->setFlash(__('El avance se ha guardado con éxito'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('El avance no se pudo guardar. Por favor, intente denuevo.'));
			}
		}
		$users = $this->Upload->User->find('list');
		$users = $this->Upload->User->find('list');
		$this->set(compact('users', 'users'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Upload->exists($id)) {
			throw new NotFoundException(__('Invalid upload'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Upload->save($this->request->data)) {
				$this->Session->setFlash(__('The upload has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The upload could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Upload.' . $this->Upload->primaryKey => $id));
			$this->request->data = $this->Upload->find('first', $options);
		}
		$users = $this->Upload->User->find('list');
		$sharedUsers = $this->Upload->SharedUser->find('list');
		$this->set(compact('users', 'sharedUsers'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Upload->id = $id;
		if (!$this->Upload->exists()) {
			throw new NotFoundException(__('Invalid upload'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Upload->delete()) {
			$this->Session->setFlash(__('Upload deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Upload was not deleted'));
		$this->redirect(array('action' => 'index'));
	}

	function uploadFile() {
  		$file = $this->data['Upload']['file'];
  		if ($file['error'] === UPLOAD_ERR_OK) {
    			$id = String::uuid();
    			if (move_uploaded_file($file['tmp_name'], APP.'uploads'.DS.$id)) {
      				$this->request->data['Upload']['id'] = $id;
      				$this->request->data['Upload']['user_id'] = $this->Auth->user('id');
      				$this->request->data['Upload']['filename'] = $file['name'];
      				$this->request->data['Upload']['filesize'] = $file['size'];
      				$this->request->data['Upload']['filemime'] = $file['type'];
				//$this->data['Preview']['upload_id']=$id;
      				return true;
    			}
  		}
  		return false;
	}
	function download($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for upload', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->Upload->bindModel(array('hasOne' => array('UploadsUser')));

		$rol=$this->Auth->user('role_id');
		/*if($rol==3){		
			$upload = $this->Upload->find('first', array(
				'conditions' => array(
				'Upload.id' => $id,
				'OR' => array(
					'UploadsUser.user_id' => $this->Auth->user('id'),
					'Upload.user_id' => $this->Auth->user('id'),
					),
				)
			));
		//}
		//else
		{*/	
			$upload = $this->Upload->find('first', array(
				'conditions' => array(
				'Upload.id' => $id
				)
			));
		//}
		//print_r($upload);

		$upload = $this->Upload->find('first', array(
		'conditions' => array(
		'Upload.id' => $id
		)
		));

		if (!$upload) {
			$this->Session->setFlash(__('Invalid id for upload', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->viewClass = 'Media';
		$filename = $upload['Upload']['filename'];
		$this->set(array(
			'id' => $upload['Upload']['id'],
			//'name' => substr($filename, 0, strrpos($filename, '.')),
			'name' => $filename,
			'extension' => substr(strrchr($filename, '.'), 1),
			'path' => APP.'uploads'.DS,
			'download' => true,
		));
	}
}
